package cn.iocoder.yudao.module.member.controller.admin.userusedfeedback;

import cn.iocoder.yudao.module.member.controller.admin.jobcollection.vo.JobCollectionRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userusedfeedback.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userusedfeedback.UserUsedFeedbackDO;
import cn.iocoder.yudao.module.member.service.userusedfeedback.UserUsedFeedbackService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 用户反馈")
@RestController
@RequestMapping("/member/user-used-feedback")
@Validated
public class UserUsedFeedbackController {
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private UserUsedFeedbackService userUsedFeedbackService;

    @PostMapping("/create")
    @Operation(summary = "创建用户反馈")
    public CommonResult<Long> createUserUsedFeedback(@Valid @RequestBody UserUsedFeedbackSaveReqVO createReqVO) {
        return success(userUsedFeedbackService.createUserUsedFeedback(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户反馈")
    public CommonResult<Boolean> updateUserUsedFeedback(@Valid @RequestBody UserUsedFeedbackSaveReqVO updateReqVO) {
        userUsedFeedbackService.updateUserUsedFeedback(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户反馈")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserUsedFeedback(@RequestParam("id") Long id) {
        userUsedFeedbackService.deleteUserUsedFeedback(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserUsedFeedbackRespVO> getUserUsedFeedback(@RequestParam("id") Long id) {
        UserUsedFeedbackDO userUsedFeedback = userUsedFeedbackService.getUserUsedFeedback(id);
        return success(BeanUtils.toBean(userUsedFeedback, UserUsedFeedbackRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户反馈分页")
    public CommonResult<PageResult<UserUsedFeedbackRespVO>> getUserUsedFeedbackPage(@Valid UserUsedFeedbackPageReqVO pageReqVO) {
        PageResult<UserUsedFeedbackDO> pageResult = userUsedFeedbackService.getUserUsedFeedbackPage(pageReqVO);
        PageResult<UserUsedFeedbackRespVO> bean = BeanUtils.toBean(pageResult, UserUsedFeedbackRespVO.class);
        for (UserUsedFeedbackRespVO respVO:bean.getList()){
            MemberUserDO user = memberUserService.getUser(respVO.getUserId());
            if (Objects.nonNull(user)){
                respVO.setUserName(user.getNickname());
            }
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户反馈 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserUsedFeedbackExcel(@Valid UserUsedFeedbackPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserUsedFeedbackDO> list = userUsedFeedbackService.getUserUsedFeedbackPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户反馈.xls", "数据", UserUsedFeedbackRespVO.class,
                        BeanUtils.toBean(list, UserUsedFeedbackRespVO.class));
    }

}